package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.ws.shop.dao.SmtpDao;
import com.ws.shop.entity.Smtp;
@Repository(value="smtpDao")
public class SmtpDaoImpl extends BaseDaoImpl implements SmtpDao {

	final String findSmtpListAll = "select id,name,password,smtp,mail,port,auth,ssls,state from jvf_smtp order by id";
	public List<Smtp> findSmtpListAll() {
		return this.query(findSmtpListAll, new ResultSetExtractor<List<Smtp>>(){
			public List<Smtp> extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				List<Smtp> list = new LinkedList<Smtp>();
				Smtp smtp = null;
				while(rs.next()){
					smtp = new Smtp();
					smtp.setId(rs.getInt(1));
					smtp.setName(rs.getString(2));
					smtp.setPassword(rs.getString(3));
					smtp.setSmtp(rs.getString(4));
					smtp.setMail(rs.getString(5));
					smtp.setPort(rs.getInt(6));
					smtp.setAuth(rs.getInt(7));
					smtp.setSsls(rs.getInt(8));
					smtp.setState(rs.getInt(9));
					list.add(smtp);
				}
				return list;
			}
		});
	}
	
	final String getSystemSmtpList = "select id,name,password,smtp,mail,port,auth,ssls from jvf_smtp where state=?";
	public List<Smtp> getSystemSmtpList(int state) {
		return this.query(getSystemSmtpList, new ResultSetExtractor<List<Smtp>>(){
			public List<Smtp> extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				List<Smtp> list = new LinkedList<Smtp>();
				Smtp smtp = null;
				while(rs.next()){
					smtp = new Smtp();
					smtp.setId(rs.getInt(1));
					smtp.setName(rs.getString(2));
					smtp.setPassword(rs.getString(3));
					smtp.setSmtp(rs.getString(4));
					smtp.setMail(rs.getString(5));
					smtp.setPort(rs.getInt(6));
					smtp.setAuth(rs.getInt(7));
					smtp.setSsls(rs.getInt(8));
					list.add(smtp);
				}
				return list;
			}
		}, state);
	}
	
	final String loadById = "select name,password,smtp,mail,port,auth,ssls,state from jvf_smtp where id=?";
	public Smtp loadById(final int id) {
		return this.query(loadById, new ResultSetExtractor<Smtp>(){
			public Smtp extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if(!rs.next())
					return null;
				Smtp s = new Smtp();
				s.setId(id);
				s.setName(rs.getString(1));
				s.setPassword(rs.getString(2));
				s.setSmtp(rs.getString(3));
				s.setMail(rs.getString(4));
				s.setPort(rs.getInt(5));
				s.setAuth(rs.getInt(6));
				s.setSsls(rs.getInt(7));
				s.setState(rs.getInt(8));
				return s;
			}
		}, id);
	}
	
	final String save_sql = "insert into jvf_smtp(name,password,smtp,mail,port,auth,ssls,state) values (?,?,?,?,?,?,?,?)";
	final String update_sql = "update jvf_smtp set name=?,password=?,smtp=?,mail=?,port=?,auth=?,ssls=?,state=? where id=?";
	public void saveOrUpdate(Smtp smtp) {
		if(smtp.getId() == 0){
			this.update(save_sql,smtp.getName(),smtp.getPassword(),smtp.getSmtp(),smtp.getMail(),smtp.getPort(),smtp.getAuth(),smtp.getSsls(),smtp.getState());
		}else{
			this.update(update_sql,smtp.getName(),smtp.getPassword(),smtp.getSmtp(),smtp.getMail(),smtp.getPort(),smtp.getAuth(),smtp.getSsls(),smtp.getState(),smtp.getId());
		}
	}

}
